package com.cy.web;

import com.cy.bean.Cartable;
import com.cy.bean.Classtab;
import com.cy.service.CarTableService;
import com.cy.service.ClassTabService;
import com.cy.token.JwtUtil;
import com.cy.token.ResultResponse;
import com.cy.util.CacheRedisService;
import com.cy.util.PriceRange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CarTableController {
    @Autowired
    private CarTableService carTableService;
    @Autowired
    private CacheRedisService cacheRedisService;

    @GetMapping("/getCarList")
    public ResultResponse getCarList(Cartable cartable){

        int did= Integer.parseInt(cacheRedisService.get("did"));
        cartable.setSchoolid(did);

        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setCode(20000);
        resultResponse.setMessage(JwtUtil.TOKEN_SUCCESS);
        resultResponse.setData(carTableService.getCarList(cartable));
        return resultResponse;
    }

    @GetMapping("/deleteCar")
    public ResultResponse deleteCar(Cartable cartable){

        int did= Integer.parseInt(cacheRedisService.get("did"));
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setCode(20000);
        resultResponse.setMessage(JwtUtil.TOKEN_SUCCESS);
        if (carTableService.deleteCar(cartable)>0){
            Cartable cartable1=new Cartable();
            cartable1.setSchoolid(did);
            resultResponse.setData(carTableService.getCarList(cartable1));
        }
        return resultResponse;
    }

    @PostMapping("/updateCar")
    public ResultResponse updateCar(@RequestBody Cartable cartable){
        int did= Integer.parseInt(cacheRedisService.get("did"));
        cartable.setSchoolid(did);

        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setCode(20000);
        resultResponse.setMessage(JwtUtil.TOKEN_SUCCESS);

        if (carTableService.updateCar(cartable)>0){
            Cartable cartable1=new Cartable();
            cartable1.setSchoolid(did);
            resultResponse.setData(carTableService.getCarList(cartable1));
        }
        return resultResponse;
    }

    @PostMapping("/addCar")
    public ResultResponse addCar(@RequestBody Cartable cartable){
        int did= Integer.parseInt(cacheRedisService.get("did"));
        cartable.setSchoolid(did);

        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setCode(20000);
        resultResponse.setMessage(JwtUtil.TOKEN_SUCCESS);

        if (carTableService.addCar(cartable)>0){
            Cartable cartable1=new Cartable();
            cartable1.setSchoolid(did);
            resultResponse.setData(carTableService.getCarList(cartable1));
        }
        return resultResponse;
    }
}
